/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.amitycs.collabdrawing.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.amitycs.collabdrawing.ToolListener;
import org.amitycs.collabdrawing.Whiteboard;
import org.amitycs.collabdrawing.WhiteboardListener;
import org.amitycs.collabdrawing.client.Client;

/**
 *
 * @author joe
 */
public class WhiteboardPanel extends JPanel implements WhiteboardListener, MouseListener, MouseMotionListener, ToolListener {
    public static final Color BG_COLOR = Color.WHITE;
    private Client client;
    private Shape currShape;
    private boolean mouseIsDown = false;
    private Whiteboard whiteboard;
    private Tool currTool;
    double x1;
    double y1;
    double x2;
    double y2;


    public WhiteboardPanel(Whiteboard wb)
    {
        whiteboard = wb;
        whiteboard.registerListener(this);
        //currTool = new LineTool(whiteboard);
        currTool = new FreedrawTool(whiteboard);
        currTool.registerListener(this);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(BG_COLOR);
        g2.fillRect(0, 0, getWidth(), getHeight());
        
        g2.setColor(Color.BLACK);
        synchronized (whiteboard.getShapes()) {
            for (Shape s : whiteboard.getShapes())
            {
                g2.draw(s);
            }
        }
        if (currShape != null) g2.draw(currShape);
    }

    public void mouseClicked(MouseEvent e) {
        // ignore
    }

    public void mousePressed(MouseEvent e) {
        mouseIsDown = true;
        currShape = currTool.onPress(e.getX(), e.getY());
        System.out.println("mouse pressed at ("+x1+","+y1+")");
        this.repaint();
    }

    public void sendShape(Shape s)
    {
        try {
            client.sendShape(s);
        } catch (IOException ex) {
            Logger.getLogger(WhiteboardPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void mouseReleased(MouseEvent e) {
        mouseIsDown = false;
        currTool.onRelease(e.getX(), e.getY());
        this.repaint();
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //mouseIsDown = false;
        //currShape = null;
        //repaint();
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setSize(500,500);
        f.setResizable(false);
        WhiteboardPanel wbp = new WhiteboardPanel(new Whiteboard());
        wbp.addMouseListener(wbp);
        wbp.addMouseMotionListener(wbp);
        f.add(wbp);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void mouseDragged(MouseEvent e) {
            //Line2D.Double currtmp = (Line2D.Double) currShape;
            //currtmp.setLine(currtmp.getX1(), currtmp.getY1(), e.getX(), e.getY());
            currShape = currTool.onDrag(e.getX(), e.getY());
            this.repaint();
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseMoved(MouseEvent e) {
        // nothing lol
    }

    public void setClient(Client c) {
        client = c;
    }

    public void shapeAdded() {
        repaint();
    }
    
    /**
     * Sets a new tool to be used.
     * Does NOT call the current Tool's onRelease()!
     * Mouse release must be done before setting tool.
     * 
     * @param t the Tool to use from now on
     */
    public void setTool(Tool t)
    {
        currTool = t;
    }

    public void shapeDone(Shape s) {
        whiteboard.addShape(s);
        sendShape(s);
    }
}
